<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>保质期</title>
    <link rel="shortcut icon" href="#"/>
</head>
<link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
<link rel="stylesheet" th:href="@{/static/admin/style.css}" media="all">
<script th:src="@{/static/layui/layui.js}"></script>
<script type="text/html" id="indexTpl">
    {{d.LAY_TABLE_INDEX+1}}
</script>
<body class="layui-layout-body">
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <!--<div class="layui-card-header">药品保质期检查</div>-->
                <div class="layui-card-body">
                    <table class="layui-hide" id="main-table" lay-filter="main-table"></table>
                    <script type="text/html" id="toolbarDemo">

                        <span class="layui-inline" style="width: 240px;">
                                <input class="layui-input" name="param" id="param" placeholder="根据药品名称查询"
                                       autocomplete="off">
                            </span>
                        <span class="layui-btn layui-btn-sm layui-bg-red" lay-event="querySearch">
                                <i class="layui-icon">&#xe615;</i>查询
                            </span>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['table', 'form'], function () {
        var $ = layui.$;
        var table = layui.table;
        table.render({
            elem: '#main-table',
            height: 650,
            url: '/drugInfo/drugInfoQueryPage',
            method: 'POST',
            page: true,
            id: 'table-load',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {title: '序号', templet: '#indexTpl', width: 60, align: 'center'},
                    {field: 'name', title: '药品名称', algin: 'center'},
                    {field: 'warrenty', title: '保质期（月）', algin: 'center'},
                    {
                        field: 'productTime', title: '生产时间', algin: 'center',
                        templet: "<div>{{layui.util.toDateString(d.productTime,'yyyy-MM-dd')}}</div>"
                    },
                    {
                        field: 'createTime', title: '当前时间', algin: 'center',
                        templet: "<div>{{layui.util.toDateString(d.createTime,'yyyy-MM-dd')}}</div>"
                    },
                    {title: '是否过期', align: 'center', templet: isWarranty}
                ]
            ]
        });

        //监听头工具栏事件
        table.on('toolbar(main-table)', function (obj) {
            switch (obj.event) {
                case 'querySearch':
                    var param = $('#param').val();
                    table.reload('table-load', {
                        where: {
                            param: param
                        }
                    });
                    $('#param').val(param);
                    break;
            }
        });

        //计算药品是否过期
        function isWarranty(d) {
            //生产日期
            var p_time = d.productTime;
            p_time = p_time.split('-');
            p_time = parseInt(p_time[0]) * 12 + parseInt(p_time[1]);
            //获取当前时间
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var now_year_month = year * 12 + month;
            var m = now_year_month - (p_time + parseInt(d.warrenty));
            if (m >= 1) {
                return "<span style='color:red'>已过期" + m + "个月</span>";
            } else if (m == 0) {
                return "<span style='color:blue'>保质期不满一个月</span>";
            } else {
                return "<span style='color:green;'>未过期,剩余" + Math.abs(m) + "个月</span>";
            }
        }
    });
</script>
</body>
</html>
